package databaseLayer;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.HibernateUtil;

public class Member_db {

    public Integer getLoyaltyPoint(String username) {
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtil.getThreadLocalSession();
            tx = session.beginTransaction();
            Query query = session.createQuery("select m.loyaltyPoint from Member as m where username=:username");
            query.setString("username", username);
            List<Integer> queryResult = query.list();
            return queryResult.get(0);
        } catch (Exception e) {
            tx.rollback();
        } finally {
            if (session != null) {
                HibernateUtil.closeSession();
            }
        }
        return null;
    }

    public boolean isMemberExist(String username) {
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtil.getThreadLocalSession();
            tx = session.beginTransaction();
            Query query = session.createQuery("from Member where username=:username");
            query.setString("username", username);
            List queryResult = query.list();
            if (queryResult.size() == 0 || queryResult.get(0) == null) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            tx.rollback();
            e.printStackTrace();
        } finally {
            if (session != null) {
                HibernateUtil.closeSession();
            }
        }
        return false;
    }

    private Member_db() {
    }
    private static Member_db instance;

    public static Member_db getInstance() {
        if (instance == null) {
            instance = new Member_db();
        }

        return instance;
    }
}
